//
//  JYStatusOriginalView.swift
//  01.weibo
//
//  Created by 谢玉锋 on 16/7/16.
//  Copyright © 2016年 itheima.cn. All rights reserved.
//

import UIKit

class JYStatusOriginalView: UIView {

    //定义模型变量
    var status: JYStatus? {
        didSet {
        // cell将模型设置给微博原创view后,微博原创view自己来设置自己的内容
        //头像
            if let profile_image_url = status?.user?.profile_image_url {
            let imgurl = NSURL(string: profile_image_url)
                iconView.sd_setImageWithURL(imgurl, placeholderImage: UIImage(named: "avatar"))
            }
        //用户名
            nameLabel.text = status?.user?.screen_name
            //时间
            timeLabel.text = status?.created_at
            //来源
            surceLabel.text = status?.source
            //微博内容
            contentLabel.text = status?.text
            
            // 微博模型提供的数据不完整,view不能直接拿来用,还需要处理,如果多个地方要使用,这段代码需要拷贝到多个地方,当cell不停滚动的时候,是经常需要计算
            //设置会员等级
//            if let mbrank = status?.user?.mbrank {
            
//                if mbrank > 0 && 6 >= mbrank {
//                memberView.image = UIImage(named:"common_icon_membership_level\(mbrank)")
//                }else {
//                memberView.image = nil
//                }
//            }
            memberView.image = status?.user?.menberViewImage
            
            
            //设置认证类型
//            if let verified_type = status?.user?.verified_type {
//            
//                switch verified_type {
//                case 0:
//                    verifiedView.image = UIImage(named: "avatar_vip")
//                case 2,3,5:
//                    verifiedView.image = UIImage(named: "avatar_enterprise_vip")
//                case 220:
//                    verifiedView.image = UIImage(named: "avatar_grassroot")
//                default:
//                    verifiedView.image = nil
//                }
//            
//            }
            verifiedView.image = status?.user?.verified_typeImage 
            // 将微博模型的图片url设置给HMStatusPictureView,HMStatusPictureView会自己去计算宽高
            pictrueView.pictureURLs = status?.storePicutreURLs
            
            // 当没有图片的时候,HMStatusPictureView距离contentLabel的底部不需要8的间距
            // snp_updateConstraints: 前提是参照的view和参照的约束没有变化才能updateConstraints  !!!
            pictrueView.snp_updateConstraints { (make) -> Void in
                let margin = status?.storePicutreURLs == nil ? 0 : JYStatusCellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }

        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
//        backgroundColor = UIColor.randomColor()

        prepareUI()
    }

    private func prepareUI(){
        //添加子控件 (因为有内容,添加到父控件中,应该可以看到一些视图)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(surceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictrueView)
        //添加约束
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(JYStatusCellMargin)
            make.height.width.equalTo(JYStatusIcon)
        }
        //昵称UILabel
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            //参照物 用 snp_trailing
            make.leading.equalTo(iconView.snp_trailing).offset(JYStatusCellMargin)
        }
        //时间UILabel
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        //来源UILabel (注意: 方向的对应)
        surceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(JYStatusCellMargin)
        }
        //会员图标
        memberView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(JYStatusCellMargin)
            make.height.equalTo(CGFloat(14))
            make.width.equalTo(CGFloat(14))
        }
        //认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.height.equalTo(CGFloat(17))
            make.width.equalTo(CGFloat(17))
        }
        //原创微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(JYStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(JYStatusCellMargin)
            make.trailing.equalTo(self).offset(-JYStatusCellMargin)//注意这里是 - ,因为往回偏移
        }
        // 配图视图图片
        pictrueView.snp_makeConstraints { (make) -> Void in
            //这里要不要当时候看 有没有配图
            make.top.equalTo(contentLabel.snp_bottom).offset(JYStatusCellMargin)
            make.leading.equalTo(self).offset(JYStatusCellMargin)
//            make.trailing.equalTo(self).offset(-JYStatusCellMargin)
            
            
            //TODO 测试
//            make.height.equalTo(Int(arc4random_uniform(200)) + 100)
            //先设置为 0 ; 后面计算时,就是更新约束: snp_updateConstraints
             make.size.equalTo(CGSizeZero)
        }
        
        //  (重要提示: 转发微博 JYStatusOriginalView ,在cell中还不知道自己高度,所以约束不了(测试设置的高度除外) 所以要在JYStatusOriginalView 类中 自己设置高度约束,这里参照配图底部就相当于有高度了 , 因为 配图会根据 pictureURLs自动计算出高度 )
        /// 自定义的原创微博view的底部和contentLabel的底部重合,偏移8
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictrueView ).offset(JYStatusCellMargin)
        }
    }
    //懒加载子控件  总结: 懒加载等号后面,要么直接赋值对象, 要么通过闭包返回一个对象,别忘了()
    //头像
    private lazy var iconView: UIImageView = {
        let iconV = UIImageView(image: UIImage(named: "avatar"))
        //Mark - 这种做法很low
        iconV.layer.cornerRadius = 17.5
        iconV.layer.masksToBounds = true
        return iconV
    
    }()
    //昵称UILabel
     private lazy var nameLabel: UILabel = UILabel(fontSize: NamelabelFont, textColor: UIColor.darkGrayColor())
//    private lazy var nameLabel: UILabel = {
//    let nameL = UILabel()
//        nameL.font = UIFont.systemFontOfSize(NamelabelFont)
//        nameL.textColor = UIColor.darkGrayColor()
//        return nameL
//    }()
    //时间UILabel
    private lazy var timeLabel: UILabel = UILabel(fontSize: JYStatusTimeFontSize, textColor: UIColor.orangeColor())
//    private lazy var timeLabel: UILabel = {
//        let timeL = UILabel()
//        timeL.font = UIFont.systemFontOfSize(JYStatusTimeFontSize)
//        timeL.textColor = UIColor.orangeColor()
//        return timeL
//    }()
    //来源UILabel
    private lazy var surceLabel: UILabel = UILabel(fontSize: JYStatusTimeFontSize, textColor: UIColor.orangeColor())
//    private lazy var surceLabel: UILabel = {
//        let surceL = UILabel()
//        surceL.font = UIFont.systemFontOfSize(JYStatusTimeFontSize)
//        surceL.textColor = UIColor.orangeColor()
//        return surceL
//    }()
    //会员图标
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    //认证图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    // 原创微博内容
    private lazy var contentLabel: UILabel = {
        
        let contentL = UILabel()
        contentL.font = UIFont.systemFontOfSize(JYTextFontSize)
        contentL.textColor = UIColor.lightGrayColor()
        contentL.numberOfLines = 0
        return contentL
    
    }()
    private lazy var pictrueView: JYStatusPictrueView = JYStatusPictrueView()
    
}












